import type { App, Plugin } from 'vue'

// 把路径转换成驼峰命名
export const pathToCamel = (path: string): string => {
	return path.replace(/\/(\w)/g, (all, letter) => letter.toUpperCase())
}

// 获取svg图标(id)列表
export const getIconList = (): string[] => {
	const rs: string[] = []
	const list = document.querySelectorAll('svg symbol')
	for (let i = 0; i < list.length; i++) {
		rs.push(list[i].id)
	}
	return rs
}

// 获取字典Label
export const getDictLabel = (dictList: any[], dictType: string, dictValue: string) => {
	const type = dictList.find((element: any) => element.dictType === dictType)
	if (type) {
		const val = type.dataList.find((element: any) => element.dictValue === dictValue + '')
		if (val) {
			return val.dictLabel
		} else {
			return dictValue
		}
	} else {
		return dictValue
	}
}

// 获取字典数据列表
export function getDictDataList(dictList: any[], dictType: string) {
	const type = dictList.find((element: any) => element.dictType === dictType)
	if (type) {
		return type.dataList
	} else {
		return []
	}
}

// 全局组件安装
export const withInstall = <T>(component: T, alias?: string) => {
	const comp = component as any
	comp.install = (app: App) => {
		app.component(comp.name || comp.displayName, component)
		if (alias) {
			app.config.globalProperties[alias] = component
		}
	}
	return component as T & Plugin
}

const formatTwice = (str: any) => {
    // 对日期等单个时间长度的格式进行处理
    let temStr = str.toString()
    temStr = temStr.length === 2 ? temStr : '0' + temStr
    return temStr
}

export const timestampToDateString = (stamp, gap, isToday, isObj, withoutMinSec) => {
    // 时间戳转
    // stamp 统一传秒, isToady 会对字符串进行今天标示
    // 如果stamp是一个日期的话，则进行一次转换
    if (isNaN(stamp * 1)) {
      stamp = new Date(stamp.replace(/-/g, '/')).getTime()
    }

    let date = new Date(stamp)
    let year = date.getFullYear()
    let month = formatTwice(date.getMonth() + 1)
    let day = formatTwice(date.getDate())
    let hour = formatTwice(date.getHours())
    let minutes = formatTwice(date.getMinutes())
    let seconds = formatTwice(date.getSeconds())

    if (!isToday) {
      if (isObj) {
        return {
          year: year,
          month: month,
          day: day,
          hour: hour,
          minutes: minutes,
          seconds: seconds
        }
      } else {
        if (withoutMinSec) {
          return [year, month, day].join(gap)
        } else {
          return [year, month, day].join(gap) + ' ' + [hour, minutes].join(':')
        }
      }
    } else {
      // 获取今天的时间戳范围
      let toady = new Date()
      let toadyStartStamp = new Date(toady.getFullYear() + '/' + (toady.getMonth() + 1) + '/' + toady.getDate() + ' 00:00:00').getTime()
      let toadyEndStamp = toadyStartStamp + 24 * 60 * 60 * 1000

      if (stamp >= toadyStartStamp && stamp <= toadyEndStamp) {
        if (withoutMinSec) {
          return '今天'
        } else {
          return '今天 ' + [hour, minutes].join(':')
        }
      } else {
        if (isObj) {
          return {
            year: year,
            month: month,
            day: day,
            hour: hour,
            minutes: minutes,
            seconds: seconds
          }
        } else {
          if (withoutMinSec) {
            return [year, month, day].join(gap)
          } else {
            return [year, month, day].join(gap) + ' ' + [hour, minutes].join(':')
          }
        }
      }
    }
  }
